﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageOrderTable.Queries
{
    /// <summary>
    /// 首页统计
    /// </summary>
    public class GetOrderstatisticsByDaysQueries : IRequest<Json<GetOrderstatisticsByDaysDTO>>
    {

    }

    public class GetOrderstatisticsByDaysQueriesHandler : IRequestHandler<GetOrderstatisticsByDaysQueries, Json<GetOrderstatisticsByDaysDTO>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _idevicetableRepository;
        private readonly IordertableRepository _iorderRepository;
        public GetOrderstatisticsByDaysQueriesHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository idevicetableRepository, IordertableRepository iorderRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _idevicetableRepository = idevicetableRepository;
            _iorderRepository = iorderRepository;
        }
        public async Task<Json<GetOrderstatisticsByDaysDTO>> Handle(GetOrderstatisticsByDaysQueries request, CancellationToken cancellationToken)
        {
            GetOrderstatisticsByDaysDTO model = new GetOrderstatisticsByDaysDTO(0, 0, 0, 0);
            int userid = _icurrentUserServices.GetUserId();
#if DEBUG
            userid = 2;
#endif
            if (userid <= 0)
            {
                return new Json<GetOrderstatisticsByDaysDTO>() { Status=1003, Detail="请先登录", Data=model };
            }
            DateTime dt_now = DateTime.Now;
            DateTime dt_begin = DateTime.Parse(dt_now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime dt_end = DateTime.Parse(dt_now.ToString("yyyy-MM-dd 23:59:59"));

            model = await _iorderRepository.GetOrderStatisticsByDays(userid, dt_begin, dt_end, cancellationToken);
            model.needAddWineCount = await _idevicetableRepository.GetNeedAddWineCount(userid,cancellationToken);
            model.DeviceCount = await _idevicetableRepository.GetCountByUserId(userid, cancellationToken);
            return new Json<GetOrderstatisticsByDaysDTO>() { Status = 1000, Detail = "查询成功", Data = model };
        }
    }
}
